package com.mashibing.apipassenger.service.impl;

import com.mashibing.apipassenger.service.ServiceVerificationCodeRestTemplateService;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.dto.serviceverificationcode.request.VerifyCodeRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author gorkr
 * @date 2022/05/27 17:23
 **/
@Service
public class ServiceVerificationCodeRestTemplateServiceImpl implements ServiceVerificationCodeRestTemplateService {
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public ResponseResult generatorCode(int identity, String phoneNumber) {
        String url = "http://service-verification-code/verify-code/generate/" + identity + "/" + phoneNumber;

        ResponseResult result = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Object>(null, null), ResponseResult.class).getBody();
        return result;
    }

    @Override
    public ResponseResult verifyCode(int identity, String phoneNumber, String code) {

        String url = "http://service-verification-code/verify-code/verify/";

        VerifyCodeRequest verifyCodeRequest = new VerifyCodeRequest();
        verifyCodeRequest.setIdentity(identity);
        verifyCodeRequest.setPhoneNumber(phoneNumber);
        verifyCodeRequest.setCode(code);

        ResponseResult result = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(verifyCodeRequest, null), ResponseResult.class).getBody();
        return result;
    }
}
    